package cn.com.infohold.smartcity.utils;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;

import java.io.Serializable;
import java.util.concurrent.ConcurrentHashMap;

import cn.com.infohold.smartcity.R;
import cn.com.infohold.smartcity.api.bean.Message;
import cn.com.infohold.smartcity.data.XmlSettings;
import cn.com.infohold.smartcity.recceiver.ReceiverApp;


/**
 * Created by apple on 2016/12/19.
 */

public class PushUtils {
    private final Context context;
    private static ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();

    public PushUtils(Context context) {
        this.context = context;
    }

    public static PushUtils getInstance(Context context) {
        return new PushUtils(context);
    }

    public void popAll() {
        if (context == null)
            return;
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancelAll();
    }


    public void pop(String sn) {
        if (!TextUtils.isEmpty(sn) && map.containsKey(sn)) {
            int id = map.get(sn);
            pop(id);
            map.remove(sn);
        }
    }

    public void pop(int id) {
        if (context == null)
            return;
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(id);
    }

    public boolean isAllowPush() {
        return XmlSettings.getNotification();
    }

    public void push(int id, String tickerText, String contentTitle, String contentText, Serializable data) {
        if (context == null || !isAllowPush())
            return;
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent intentClick = getIntentClick(context, id, data);
        int icon = R.mipmap.ic_launcher;
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSmallIcon(icon)
                .setWhen(System.currentTimeMillis())
                .setTicker(XmlSettings.getStatusbar() ? tickerText : null)
                .setContentTitle(contentTitle)
                .setContentText(contentText)
                .setAutoCancel(true)
                .setContentIntent(intentClick)
                .setDefaults(Notification.DEFAULT_ALL);

        if (XmlSettings.getLight())
            builder.setLights(Color.GREEN, 1000, 3000);

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            builder.setVisibility(Notification.VISIBILITY_PUBLIC);
//            // 关联PendingIntent
//            builder.setFullScreenIntent(intentClick, true);// 横幅
//        }

        Notification notification = builder.build();
        if (XmlSettings.getVibrate())
            notification.defaults |= Notification.DEFAULT_VIBRATE;
        else
            notification.defaults &= ~Notification.DEFAULT_VIBRATE;

        if (XmlSettings.getSound())
            notification.defaults |= Notification.DEFAULT_SOUND;
        else
            notification.defaults &= ~Notification.DEFAULT_SOUND;

        if (!XmlSettings.getLight())
            notification.defaults &= ~Notification.DEFAULT_LIGHTS;

        manager.notify(id, notification);

    }

    public void pushMessage(Message message) {
        if (message != null && isAllowPush()) {
            int id = message.hashCode();
            push(id, message.getTitle(), message.getTitle(), message.getContent(), message);
            map.put(message.getId(), id);
        }
    }

    private Intent getIntent(Context context, Serializable message) {
        Intent intent = new Intent();
        intent.setAction(ReceiverApp.ACTION_GOTO);
        intent.putExtra("PackageName", context.getPackageName());
        intent.putExtra(Serializable.class.getName(), message);
        return intent;
    }

    public PendingIntent getIntentClick(Context context, int requestCode, Serializable message) {
        Intent intent = getIntent(context, message);
        return PendingIntent.getBroadcast(context, requestCode, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
    }

}
